﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2018-05-15 10:35:51
// 负责人：Sushee
// ===================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;
using System.Text;
namespace OF.Component.DAL
{
	#region  数据访问层
	/// <summary>
	/// 数据访问层
	/// </summary>	
	internal partial class AuthorityMenu:IAuthorityMenu
	{		
    
            //连接字符串
            public virtual string ConnectionStringKey
            {
                get
                {
                    return ConstantUtility.ConnectionEjautobaseKey;
                }
            }
        #region GetMaxID
        
    
      
            public int GetMaxID()
            {
                int nReturn=0;
                string szSql=@"SELECT ISNULL(MAX(MenuID),0)+1
                               FROM [AuthorityMenu]";             
                Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
                DbCommand dbc = db.GetSqlStringCommand(szSql);
                try
                {
                    object o = db.ExecuteScalar(dbc);
                    if (o!=null)
                    {
                        nReturn = Convert.ToInt32(o);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return nReturn;               
            }
            
      
        
        #endregion
    
		#region Basic CRUD functions
         private AuthorityMenuInfo GetByReader(IDataReader dr)
         {
            AuthorityMenuInfo info = new AuthorityMenuInfo();
            object obj;
            obj=dr["MenuID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.MenuID = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["ParentID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.ParentID = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["MenuName"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.MenuName = obj.ToString();
            }
            obj=dr["MenuUrl"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.MenuUrl = obj.ToString();
            }
            obj=dr["SystemID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.SystemID = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["Remark"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.Remark = obj.ToString();
            }
            obj=dr["CreateDateTime"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.CreateDateTime = ((obj)==DBNull.Value)?Convert.ToDateTime("1900-1-1"):Convert.ToDateTime(obj);
            }
            obj=dr["UpdateTime"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.UpdateTime = ((obj)==DBNull.Value)?Convert.ToDateTime("1900-1-1"):Convert.ToDateTime(obj);
            }
            obj=dr["IsActive"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.IsActive = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["OperationID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.OperationID = (Guid)obj;
            }
            obj=dr["OrderNum"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.OrderNum = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["MenuCode"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.MenuCode = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            return info;
         }
		public int Insert(AuthorityMenuInfo info)
		{
        
        
            string strSql=@"INSERT INTO AuthorityMenu			
			(			
			[MenuID],
			[ParentID],
			[MenuName],
			[MenuUrl],
			[SystemID],
			[Remark],
			[CreateDateTime],
			[UpdateTime],
			[IsActive],
			[OperationID],
			[OrderNum],
			[MenuCode]
			)
			 VALUES
			(			
			@MenuID,
			@ParentID,
			@MenuName,
			@MenuUrl,
			@SystemID,
			@Remark,
			@CreateDateTime,
			@UpdateTime,
			@IsActive,
			@OperationID,
			@OrderNum,
			@MenuCode
			)";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql);
            db.AddInParameter(dbc, "@MenuID",DbType.Int32, info.MenuID);
            db.AddInParameter(dbc, "@ParentID",DbType.Int32, info.ParentID);
            db.AddInParameter(dbc, "@MenuName",DbType.String, info.MenuName);
            db.AddInParameter(dbc, "@MenuUrl",DbType.String, info.MenuUrl);
            db.AddInParameter(dbc, "@SystemID",DbType.Int32, info.SystemID);
            db.AddInParameter(dbc, "@Remark",DbType.String, info.Remark);
            db.AddInParameter(dbc, "@CreateDateTime",DbType.DateTime, info.CreateDateTime.ToNullDbDateTime());
            db.AddInParameter(dbc, "@UpdateTime",DbType.DateTime, info.UpdateTime.ToNullDbDateTime());
            db.AddInParameter(dbc, "@IsActive",DbType.Int32, info.IsActive);
            db.AddInParameter(dbc, "@OperationID",DbType.Guid, info.OperationID);
            db.AddInParameter(dbc, "@OrderNum",DbType.Int32, info.OrderNum);
            db.AddInParameter(dbc, "@MenuCode",DbType.Int32, info.MenuCode);

             
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
            return (int)db.GetParameterValue(dbc,"MenuID");
         
		}
		
		
        public void Update(AuthorityMenuInfo info)
		{
            StringBuilder sbSql=new StringBuilder("UPDATE AuthorityMenu SET ");
            sbSql.AppendLine("[ParentID] = @ParentID,");
                
            sbSql.AppendLine("[MenuName] = @MenuName,");
                
            sbSql.AppendLine("[MenuUrl] = @MenuUrl,");
                
            sbSql.AppendLine("[SystemID] = @SystemID,");
                
            sbSql.AppendLine("[Remark] = @Remark,");
                
            sbSql.AppendLine("[CreateDateTime] = @CreateDateTime,");
                
            sbSql.AppendLine("[UpdateTime] = @UpdateTime,");
                
            sbSql.AppendLine("[IsActive] = @IsActive,");
                
            sbSql.AppendLine("[OperationID] = @OperationID,");
                
            sbSql.AppendLine("[OrderNum] = @OrderNum,");
                
            sbSql.AppendLine("[MenuCode] = @MenuCode");
                
            sbSql.Append(" where  [MenuID] = @MenuID");
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(sbSql.ToString());
            db.AddInParameter(dbc, "@MenuID",DbType.Int32, info.MenuID);
            db.AddInParameter(dbc, "@ParentID",DbType.Int32, info.ParentID);
            db.AddInParameter(dbc, "@MenuName",DbType.String, info.MenuName);
            db.AddInParameter(dbc, "@MenuUrl",DbType.String, info.MenuUrl);
            db.AddInParameter(dbc, "@SystemID",DbType.Int32, info.SystemID);
            db.AddInParameter(dbc, "@Remark",DbType.String, info.Remark);
            db.AddInParameter(dbc, "@CreateDateTime",DbType.DateTime, info.CreateDateTime.ToNullDbDateTime());
            db.AddInParameter(dbc, "@UpdateTime",DbType.DateTime, info.UpdateTime.ToNullDbDateTime());
            db.AddInParameter(dbc, "@IsActive",DbType.Int32, info.IsActive);
            db.AddInParameter(dbc, "@OperationID",DbType.Guid, info.OperationID);
            db.AddInParameter(dbc, "@OrderNum",DbType.Int32, info.OrderNum);
            db.AddInParameter(dbc, "@MenuCode",DbType.Int32, info.MenuCode);
			
           
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
		}
		
        public void Delete(int menuID)
		{
			StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM [AuthorityMenu]  ");
            strSql.Append("WHERE [MenuID]=@MenuID ");
           
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
		    db.AddInParameter(dbc, "@MenuID",DbType.Int32, menuID);
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
          
           
		}
        
         public IList<AuthorityMenuInfo> Search(out int pageCount, int pageIndex, int pageSize, string where, string orderField, bool isDesc)
        {
            IList<AuthorityMenuInfo> list = new List<AuthorityMenuInfo>();
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetStoredProcCommand("sp_OF_Page");
            //设置参数
            db.AddOutParameter(dbc, "@pageCount", DbType.Int32, 4);
            db.AddInParameter(dbc, "@pageIndex", DbType.Int32, pageIndex);
            db.AddInParameter(dbc, "@pageSize", DbType.Int32, pageSize);
            db.AddInParameter(dbc, "@strWhere", DbType.String, where);
            db.AddInParameter(dbc, "@fldName", DbType.String, orderField);
            db.AddInParameter(dbc, "@OrderType", DbType.Boolean, isDesc);
            db.AddInParameter(dbc, "@tblName", DbType.String, "AuthorityMenu");
            db.AddInParameter(dbc, "@strGetFields", DbType.String, "*");
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                while (dr.Read())
                {
                    AuthorityMenuInfo info = GetByReader(dr);
                    list.Add(info);
                }
            }
            pageCount = (int)db.GetParameterValue(dbc, "@PageCount");
            return list;
        }
        
      
        public AuthorityMenuInfo GetByID(int menuID)
		{
            Model.AuthorityMenuInfo  info = null;
		    StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT  * FROM [AuthorityMenu] ");
            strSql.Append("WHERE [MenuID]=@MenuID ");
           
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
		    db.AddInParameter(dbc, "@MenuID",DbType.Int32, menuID);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                if (dr.Read())
                {
                    info = GetByReader(dr);
                }
            }
            return info;
		}
		
        
		#endregion
	}
	#endregion
}